package qlsl.androiddesign.http.service.subservice;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import net.sourceforge.pinyin4j.PinyinHelper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import qlsl.androiddesign.constant.SoftwareConstant;
import qlsl.androiddesign.constant.WhatConstant;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.CarBrand;
import qlsl.androiddesign.entity.otherentity.CarDetail;
import qlsl.androiddesign.entity.otherentity.CarModel;
import qlsl.androiddesign.entity.otherentity.FileType;
import qlsl.androiddesign.entity.otherentity.Machine;
import qlsl.androiddesign.handler.subhandler.HttpHandler;
import qlsl.androiddesign.http.HttpListener;
import qlsl.androiddesign.http.service.baseservice.BaseService;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.thread.HandlerThread;
import qlsl.androiddesign.util.commonutil.PagerUtils;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 二手车市场模块 <br/>
 * 
 */
public class CarService extends BaseService {

	private static String className = getClassName(CarService.class);

	/**
	 * 查询二手车列表
	 */
	public static void queryUsedCarList(final String brands,
			final String province, final String type, final int page,
			final FunctionView<?> functionView, final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryUsedCarList");
		new HandlerThread(className, "queryUsedCarList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("usedcarlist")
							.addParam("brands", brands)
							.addParam("province", province)
							.addParam("type", type).addParam("pageindex", page)
							.addParam("pagesize", SoftwareConstant.PAGER_SIZE)
							.addParam("condition", 0).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<CarDetail> list = JSONArray.parseArray(
							jo.getString("rows"), CarDetail.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 查询个人发布二手车列表
	 */
	public static void queryUsedCarList(final int page,
			final FunctionView<?> functionView, final HttpListener listener) {
		functionView.setProgressBarText("正在查询二手车");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryUsedCarList");
		new HandlerThread(className, "queryUsedCarList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("usedcarlist")
							.addParam("pageindex", page)
							.addParam("pagesize", SoftwareConstant.PAGER_SIZE)
							.addParam("condition", 1).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<CarDetail> list = JSONArray.parseArray(
							jo.getString("rows"), CarDetail.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 查询二手车图片流<br/>
	 */
	public static void queryUsedCarPhotoList(final String usedcarid,
			final FunctionView<?> functionView, final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryUsedCarPhotoList");
		new HandlerThread(className, "queryUsedCarPhotoList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("usedcarphotos")
							.addParam("usedcarid", usedcarid).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					List<FileType> list = JSONArray.parseArray(
							jo.getString("rows"), FileType.class);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, list);

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 收藏二手车<br/>
	 */
	public static void storeUsedCar(final String usedcarid,
			final FunctionView<?> functionView, final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener,
				className, "storeUsedCar");
		new HandlerThread(className, "storeUsedCar") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("collectusedcar")
							.addParam("usedcarid", usedcarid).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, "收藏成功");

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 取消收藏二手车<br/>
	 */
	public static void uncollectUsedCar(final String usedcarid,
			final FunctionView<?> functionView, final HttpListener listener) {
		functionView.setProgressBarText("正在取消收藏");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "uncollectUsedCar");
		new HandlerThread(className, "uncollectUsedCar") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("cancelcollectusedcar")
							.addParam("collectid", usedcarid).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					functionView.activity.runOnUiThread(new Runnable() {

						public void run() {
							functionView.showToast("取消收藏成功");
							queryCollectCarList(1, functionView, listener);
						}
					});

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 查询品牌列表[用于PopupWindow]<br/>
	 */
	public static void queryBrandList(final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在查询品牌");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryBrandList");
		new HandlerThread(className, "queryBrandList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getbrandslist").post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					List<CarBrand> list = JSONArray.parseArray(
							jo.getString("rows"), CarBrand.class);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, list);

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 查询品牌列表[用于卖车]<br/>
	 */
	public static void queryBrand(final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在查询品牌");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryBrandList");
		new HandlerThread(className, "queryBrandList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getbrandslist").post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<CarBrand> list = JSONArray.parseArray(
							jo.getString("rows"), CarBrand.class);

					Pager pager = PagerUtils.createPager(1, jo);
					map.put("list", list);
					map.put("pager", pager);

					for (CarBrand car : list) {
						String currentLetter = PinyinHelper
								.toHanyuPinyinStringArray(car.getName().charAt(
										0))[0].charAt(0)
								+ "";
						car.setInitials(currentLetter.toUpperCase());

					}
					Collections.sort(list);
					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 查询型号<br/>
	 */
	public static void queryModelList(final String fbrandsid,
			final FunctionView<?> functionView, final HttpListener listener) {
		functionView.setProgressBarText("正在查询型号");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryModelList");
		new HandlerThread(className, "queryModelList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getmodelnolist")
							.addParam("fbrandsid", fbrandsid).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<CarModel> list = JSONArray.parseArray(
							jo.getString("rows"), CarModel.class);
					Pager pager = PagerUtils.createPager(1, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 查询二手车收藏列表
	 */
	public static void queryCollectCarList(final int page,
			final FunctionView<?> functionView, final HttpListener listener) {
		functionView.setProgressBarText("正在查询二手车");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryCollectCarList");
		new HandlerThread(className, "queryCollectCarList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("usedcarcollectlist")
							.addParam("pageindex", page)
							.addParam("pagesize", SoftwareConstant.PAGER_SIZE)
							.post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<CarDetail> list = JSONArray.parseArray(
							jo.getString("rows"), CarDetail.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 查询机型<br/>
	 */
	public static void queryMachineList(final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在查询机型");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryMachineList");
		new HandlerThread(className, "queryMachineList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getmachineversionlist")
							.post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<Machine> list = JSONArray.parseArray(
							jo.getString("rows"), Machine.class);
					Pager pager = PagerUtils.createPager(1, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}
}
